package com.exemplo.flickrsocial.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.exemplo.flickrsocial.persistence.SharedFlickr;
import com.gmail.yuyang226.flickr.FlickrException;
import com.gmail.yuyang226.flickr.oauth.OAuth;
import com.gmail.yuyang226.flickr.oauth.OAuthToken;
import com.gmail.yuyang226.flickr.people.User;

public class FlickrSocialModel {

	private Context context;

	public FlickrSocialModel(Context contex) {

		this.context = contex;
	}

	public OAuth getOAuthToken() {

		String oauthTokenString = SharedFlickr.getOauthToken();
		String tokenSecret = SharedFlickr.getOauthTokenSecrent();

		if (oauthTokenString == null && tokenSecret == null) {
			Log.w(getClass().toString(), "No oauth token retrieved");
			return null;
		}
		OAuth oauth = new OAuth();
		String userName = SharedFlickr.getUserName();
		String userId = SharedFlickr.getUserID();

		if (userId != null) {
			User user = new User();
			user.setUsername(userName);
			user.setId(userId);
			oauth.setUser(user);
		}

		OAuthToken oauthToken = new OAuthToken();
		oauth.setToken(oauthToken);
		oauthToken.setOauthToken(oauthTokenString);
		oauthToken.setOauthTokenSecret(tokenSecret);
		return oauth;
	}

	public OAuth onOAuthDone(OAuth result) {
		if (result == null) {
			Toast.makeText(context, "Authorization failed", Toast.LENGTH_LONG)
					.show();
			return null;

		} else {
			User user = result.getUser();
			OAuthToken token = result.getToken();
			if (user == null || user.getId() == null || token == null
					|| token.getOauthToken() == null
					|| token.getOauthTokenSecret() == null) {
				Toast.makeText(context, "Authorization failed",
						Toast.LENGTH_LONG).show();
			}

			SharedFlickr.savedOauthToken(user.getUsername(), user.getId(),
					token.getOauthToken(), token.getOauthTokenSecret());

			return result;
		}

	}

	public void managesCreadSharedPreferences() throws FlickrException {

		if (context != null) {

			SharedFlickr.sharedCreate(context);

		}
	}

	public void managesSaveOAuthToken(String userName, String userId,
			String token, String tokenSecret) {

		SharedFlickr.savedOauthToken(userName, userId, token, tokenSecret);
	}

	public void saveImageGallery(ImageView image) {

		File rootsd = Environment.getExternalStorageDirectory();
		File file = new File(rootsd.getAbsolutePath()
				+ Environment.DIRECTORY_PICTURES);

		image.setDrawingCacheEnabled(true);

		image.buildDrawingCache(true);
		Bitmap bitMap = image.getDrawingCache();

		try {

			file.createNewFile();
			FileOutputStream ostream = new FileOutputStream(file);
			bitMap.compress(CompressFormat.JPEG, 80, ostream);
			image.setDrawingCacheEnabled(false);
			ostream.close();

		} catch (IOException e) {

			e.printStackTrace();
		}

	}
}
